﻿var paginationTotal = 20;//记录总数
var isSearch = false;
function submitsearch() {
    isSearch = true;
    paginationCurrentPage = 1;
    initData(0);
}

function loadData(page, pageSize) {
    var vForm = "";
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serializeArray();
    }
    loading();
    $.ajax({ //这里使用到Jquery的ajax方法
        type: "POST",
        async: false,
        dataType: "json",
        url: "/user/action/list/page/" + paginationCurrentPage + "/pageSize/" + paginationPageSize,
        data: vForm,
        success: function(json) {
            closeLoading();
            var list = "";
            var module = '';
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='9' class=\"center\">&nbsp;暂无数据</td>";
            } else {
                var i = paginationCurrentPage == 1 ? 1 : paginationPageSize * (paginationCurrentPage - 1) + 1;
                $.each(json.result, function(key, val) {
                    if (val.ura_module != module) {
                        module = val.ura_module;
                        //if((key+1)%2 ==1) {list += "<tr class=''>";} else {list += "<tr class='even-tr'>";}
                        list += "<tr class='even-tr'>";
                        list += "<td colspan='9'>模块名：" + module + "</td>";
                        list += "</tr>";
                    }
                    var statusIcon = val.ura_status == 0 ? "unnormal.png" : "normal.png";
                    var displayIcon = val.ura_display == 0 ? "unnormal.png" : "normal.png";
                    list += "<tr class=''>";
                    list += "<td class=\"center\">" + (i++) + "</td>";
                    list += "<td>" + val.ura_title + "</td>";
                    list += "<td>" + val.ura_title_alias + "</td>";
                    list += "<td>" + val.ura_module + "</td>";
                    list += "<td>" + val.ura_controller + "</td>";
                    list += "<td>" + val.ura_action + "</td>";
                    list += "<td class=\"center\"><img src=\"/images/icon/" + displayIcon + "\"></td>";
                    list += "<td class=\"center\"><img src=\"/images/icon/" + statusIcon + "\"></td>";
                    list += "<td class=\"center\"><a href=\"javascript:edit(" + val.ura_id + ")\"><img src=\"/images/icon/edit.gif\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:delelteInfo(" + val.ura_id + ")\"><img src=\"/images/icon/delete.gif\"></a></td>";
                    list += "</tr>";
                });
            }
            $("#loadListData").html(list);
        }
    });
}

function loadSelect() {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/user/action/select",
        data: null,
        success: function(json) {
            var list = "<option value=''>All</option>";
            if (json.ask == 1) {
                $.each(json.result, function(key, val) {
                    list += "<option value=" + val.ura_module + ">" + val.ura_module + "</option>";
                });
            }
            $("#sModule").html(list);
        }
    });
}

//删除
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}

function dialogclose() {
    $("#dialog").dialog("close");
}

$(function() {
    initData(0);
    loadSelect();//模块下拉选项
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons: {
            '确定': function() {
                if ($("#createForm").valid()) {
                    create();
                }
            },
            '取消': function() {
                tip('');
                $(this).dialog('close');
            }
        }
    });

    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons: {
            '确定': function() {
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: "/user/action/delete",
                    data: "ura_id=" + Uid,
                    success: function(json) {
                        if (json.ask == '1') {
                            initData(0);
                            messags = "成功删除！";
                        } else {
                            messags = "删除失败！";
                        }
                        alertTip(messags);
                    }
                });
                $(this).dialog('close');
            },
            '取消': function() {
                $(this).dialog('close');
            }
        }

    });

    $("#create").click(function() {
        tip('');
        $("#dialog").dialog("open");
        $("#createForm input").each(function() {
            $(this).val('');
        });
    });
    $("#close").click(function() {
        dialogclose();
    });

    //表单验证
    $("#createForm").validate({
        errorPlacement:function(error, element) {
            element.parent().children(".errormsg").html(error);
        },
        messages: {
            ura_title: "<span><img src=\"/images/no.gif\">必填项!</span>",
            ura_module: "<span><img src=\"/images/no.gif\">必填项!</span>",
            ura_controller: "<span><img src=\"/images/no.gif\">必填项!</span>",
            ura_action: "<span><img src=\"/images/no.gif\">必填项!</span>"
        }
    });
});
//修改、添加
function create() {
    vForm = "";
    vForm = $("#createForm").serializeArray();
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/user/action/create",
        data: vForm,
        success: function(json) {
            if (json.ask != '0') {
                if (json.ask == 1) {
                    initData(0);
                    alertTip("添加成功!");
                    dialogclose();
                } else if (json.ask == 2) {
                    loadData(paginationCurrentPage, paginationPageSize);
                    alertTip("修改成功!");
                    dialogclose();
                } else {
                    tip("你没有修改任何资料！");
                }
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                tip(html);
            }
        }
    });
}

function tip(mess) {
    var tips = $("#tips");
    tips.html(mess);
    if (mess != '') {
        tips.show();
    } else {
        tips.hide();
    }
    $(".errormsg").each(function() {
        $(this).html('*');
    });
}
//操作提示
function alertTip(tip) {
    $('<div title="提示"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '确定': function() {
                $(this).dialog("close");
            }
        }
    });
}
function edit(id) {
    tip('');
    loadSelect(0);
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/user/action/info",
        data: "ura_id=" + id,
        success: function(json) {
            if (json != '' && json != null) {
                $("[name=ura_title]").val(json.ura_title);
                $("[name=ura_title_alias]").val(json.ura_title_alias);
                $("[name=ura_module]").val(json.ura_module);
                $("[name=ura_status]").val(json.ura_status);
                $("[name=ura_display]").val(json.ura_display);
                $("[name=ura_controller]").val(json.ura_controller);
                $("[name=ura_action]").val(json.ura_action);
                $("[name=ura_id]").val(json.ura_id);
                $("#dialog").dialog("open");
            }
        }
    });
}

//模块、控制器、方法级联搜索
function listMCA(t, v) {
    $.getJSON("/user/action/selectmvc", {t:t,v:v}, function(json) {
        var optionStr = '<option value="">ALL</option>';
        var controller = '';
        if (json.ask == 1) {
            $.each(json.result, function(k, v) {
                if (t == 'contro') {
                    if (controller != v.ura_controller) {
                        controller = v.ura_controller;
                        optionStr += '<option value="' + v.ura_controller + '">' + v.ura_controller + '</option>';
                    }
                } else if (t == 'action' && (v.ura_module == $('#sModule').val())) {
                    optionStr += '<option value="' + v.ura_action + '">' + v.ura_action + '</option>';
                }
            });
        }
        if (t == 'contro') {
            $("#sAction").html('<option value="">ALL</option>');
            $("#sContro").html(optionStr);
        } else if (t == 'action') {
            $("#sAction").html(optionStr);
        }
        submitsearch();
    });
}